//基础列表页面，不含状态筛选与添加按钮
var basePage = require('../../common/BasePage');
var baseList = require('../../common/BaseList');
Page({
  behaviors: [basePage, baseList],

  /**
   * 页面的初始数据
   */
  data: {
    checkMobile: false,
    /**是否播放视频/视频地址 */
    showVideo: false,
    search: "",
    reqUrl: '/home/yuyue/index',
    /**预支付地址 */
    preUrl: '/home/ump/sweep/preinfo',
    /**购买弹窗 */
    addCart: false,
    /**购买数量 */
    nums: 1,
    /**选中的产品 */
    pdc: {},
    /**分享链接 */
    path: '/pages/cls/list/BaseList',
    /**页面标题 */
    title: '',
    //预售
    showSubscribeClick: false,
    radioSale: -1,
    checkedSale: false,
    bookPdc: "",
    booknums: 1,
    booksku: 1,
    wh_id: 1,
    article_content: "",
    active: 0,
    catIndex: 0, //分类索引
    catData: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let wh_id = options.wh_id || getApp().globalData.warehouse.wh_id;
    let param = {
      wh_id: wh_id
    }
    this.setData({
      wh_id: wh_id,
      reqData: param
    })

    this.request({
      url: this.data.host + '/home/productCate?wh_id='+wh_id+"&from=yuyue&is_seckill=0&live_stream=0"
    }, this.get_cate_ok);

    // let catArr = getApp().globalData.cateArr;
    // console.log("catArr", catArr)
    // if (!catArr || !catArr.length) { //没有分类缓存
    //   // 请求分类列表
    //   return this.request({
    //     url: this.data.host + '/home/product/cate?limit=1000'
    //   }, this.get_cate_ok);
    // } else {
    //   this.setData({
    //     catData: catArr
    //   })
    //   this.init_pdc()
    // }
    // this.req_items();
    this.request({
      url: this.data.host + '/home/article/detail?article_id=31'
    }, this.get_detail_ok);
  },
  /**下拉刷新 */
  onPullDownRefresh() {
    console.log("下拉刷新");
    this.data.current_page = 0;
    this.request({
      url: this.data.host + '/home/productCate?wh_id='+this.data.wh_id+"&from=yuyue&is_seckill=0&live_stream=0"
    }, this.get_cate_ok);
  },
  init_pdc() {
    // console.log("this.data.catIndex",this.data.catIndex)
		let data = this.data;
		this.data.reqData = {
      wh_id:this.data.wh_id,
      cate_id: data.catData[this.data.catIndex].cate_id,
    }
    
    data.current_page = 0;

    this.req_items();
  },
	get_cate_ok(res) {
    //  console.log("get_cate_ok", res);
    // getApp().globalData.cateArr = res.data;
    this.setData({
      catData: res.data
    })
    if (res.data && res.data.length) {

      this.init_pdc()
    } 
    // else {
    //   wx.showToast({
    //     title: '暂无产品',
    //     icon: 'none'
    //   })
    // }
	},
	cat_change(e) {
    // console.log("cat_change", e);
    this.setData({
      catIndex: e.detail
    });

    this.init_pdc();
  },
  get_detail_ok(res) {
    console.log("get_detail_ok", res);
    this.setData({
      article_content: res.data.article_content.content.replace(/<img /ig, '<img width="100%" ')
    });
    // wx.setNavigationBarTitle({
    //   title: res.data.title,
    // })
  },
  onChangeTab(e) {
    console.log("e", e)
    this.setData({
      active: e.detail.index
    })
  },
  search_click(e) {
    console.log("search_click", e);
    this.data.current_page = 0;
    this.data.reqData.so = e.detail;
    // this.data.reqData = {
    //   so: e.detail
    // }
    this.req_items();
  },
  login_reload() {
    this.req_items();
  },
  nums_change(e) {
    this.data.nums = e.detail;
    // console.log("nums_change",e)
  },
  /**
   * 下预约单
   */
  add_cart() {
    this.request({
      url: this.data.host + this.data.preUrl,
      data: {
        presell_id: this.data.pdc.presell_id,
        sweep_id: this.data.pdc.sweep_id,
        seckill_id: this.data.pdc.seckill_id,
        nums: this.data.nums,
        wh_id: this.data.wh_id
      },
      method: 'GET'
    }, this.add_preinfo_ok);
  },
  add_preinfo_ok(res) {
    // console.log('add_preinfo_ok', res);
    let noShowNum = this.data.title == "斗南专享" ? '1' : ''; //斗南专享下单时不提示起购数量
    wx.navigateTo({
      url: '/pages/preSaleList/preinfo/Order?noShowNum=' + noShowNum,
      success: function (obj) {
        // 通过eventChannel向被打开页面传送数据
        obj.eventChannel.emit('orderData', {
          data: res.data,
          m_addr: res.data.m_addr
        })
      }
    })
  },

  init_val(val, index, arr) {
    val.thumb = this.get_thumb(val.product_img);
    val.price = val.miao_price || val.sweep_price || val.presell_price;
    if (val.seckill_id) {
      val.sku = val.total_num - val.sales;
      if (val.sku < 0) {
        val.sku = 0;
      }
    }

  },

  /**
   * 播放视频
   */
  video_click(e) {
    this.setData({
      showVideo: this.data.imgHost + '/' + e.detail.video
    })
  },

  subscribeClick_click(e) {
    console.log("预订", e)
    this.setData({
      radioSale: -1,
      showSubscribeClick: true,
      bookPdc: e.detail,
      booknums: 1,
      booksku: 1
    })
  },

  //跳转详情
  toDetail(e){
    console.log("详情",e)
    var info=e.detail
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?from=yuyue&supplier_id='+info.supplier_id+
      "&pid="+info.yuyue_id,
    })
  },

  onClose() {
    this.setData({
      showSubscribeClick: false
    })
  },
  //查看预售协议
  go_article(e) {
    wx.navigateTo({
      url: '/pages/article/Index?article_id=31',
    })
  },
  //选择预订
  onChangeSale(e) {

    let preArr = this.data.bookPdc.sku_item
    let index = e.detail
    console.log("选择预订", preArr[index].sku)
    this.setData({
      booksku: preArr[index].sku,
      radioSale: index
    })
  },
  booknums_change(e) {
    // this.data.nums = e.detail;
    this.setData({
      booknums: e.detail
    })
    console.log("booknums_change", e)
  },
  //协议
  onChangeSaleAgreement() {
    let checkedSale = this.data.checkedSale
    checkedSale = !checkedSale
    this.setData({
      checkedSale: checkedSale
    })
  },
  //确定
  saleBtn_click(e) {
    console.log("确定", e)
    let state = e.currentTarget.dataset.state;
    if (state != 0) {
      // if(!this.data.checkedSale){
      // 	return wx.showToast({
      // 		title: '请阅读并同意预售交易协议',
      // 		icon: "none"
      // 	})
      // }
      if (this.data.radioSale == -1) {
        return wx.showToast({
          title: '请选择预售商品',
          icon: "none"
        })
      }
      console.log("确定radioSale", this.data.radioSale)
      let param = {
        yy_id: this.data.bookPdc.sku_item[this.data.radioSale].yy_id,
        nums: this.data.booknums
      }
      this.request({
        url: this.data.host + '/home/yuyue/preinfoNew',
        data: param,
        method: 'get'
      }, res => {
        res.data.thumb = this.get_thumb(res.data.product_img, 'wide');
        let that = this;
        wx.navigateTo({
          url: '/pages/neworder_confirm/order_confirm?wh_id=' + this.data.wh_id + "&from=yuyue",
          success: function (obj) {
            // 通过eventChannel向被打开页面传送数据

            obj.eventChannel.emit('orderData', {
              wh_id: that.data.wh_id,
              data: res.data,
              m_addr: res.data.m_addr,
              activeCar: "yuyue",
              wuliu: res.data.freight_list,
              specialList: res.data.extra_list,
              pack_list: res.data.pack_list,
              buyType: 2,
              order_pei_type: res.order_pei_type,
              orderWarehouse: res.warehouse
            })
          }
        })

      });

    }
    this.setData({
      showSubscribeClick: false
    })


  },
  nums_change(e) {
    // this.data.nums = e.detail;
    this.setData({
      nums: e.detail
    })
    console.log("nums_change", e)
  },

})